<?php

/**
 *  后台继承类
 * @file   Admin.php  
 * @date   2016-8-23 19:45:21 
 * @author Zhenxun Du<5552123@qq.com>  
 * @version    SVN:$Id:$ 
 */

namespace app\index\controller;
use app\admin\model\Section as SectionModel;
use think\Config;


class Section extends Common {


    //pc获取断面列表
    public function index(){
        $post = $this->postData();

        $where = [];
        if(isset($post['posname']) && $post['posname']){
            $where['posname'] = ['like',"%{$post['posname']}%"];
        }
        if(isset($post['com_id']) && $post['com_id']){
            $where['com_id'] = $post['com_id'];
        }
        $field = "A.*,B.name as company_name";
        $lists = db("section")->alias("A")->field($field)
            ->join("cy_company B","A.com_id=B.id","left")

            ->where($where)->order("A.id desc")->paginate(20);

        $taskTypeArr = Config::get('task_type');
        $company = db('company')->select();

        $this->assign('company', $company);
        $this->assign('lists', $lists);
        $this->assign('posname', $post['posname']);
        $this->assign('com_id', $post['com_id']);
        $this->assign('taskTypeArr', $taskTypeArr);
        return $this->fetch();
    }

    public function info() {

        $id = input('id');

        $company = db('company')->select();

        $province = db('region')->where(['parent_id'=>0])->select();
        if ($id) {
            //当前用户信息
            $info = db('section')->where(['id'=>$id])->find();
            $this->assign('info', $info);

            $cityRow = db('region')->where(['name'=>$info['province']])->find();
            $city = db('region')->where(['parent_id'=>$cityRow['id']])->select();

            $countryRow = db('region')->where(['name'=>$info['city']])->find();
            $country = db('region')->where(['parent_id'=>$countryRow['id']])->select();

            $this->assign('city', $city);
            $this->assign('country', $country);
        }

        $taskType = Config::get('task_type');
        $this->assign('taskType', $taskType);
        $this->assign('company', $company);
        $this->assign('province', $province);
        $this->assign('id', $id);

        return $this->fetch();
    }

    public function getRegion(){
        $parent_id = input('parent_id');
        $province = db('region')->where(['parent_id'=>$parent_id])->select();
        return json_encode($province);
    }
    public function add(){
        $post = $this->postData();

        if(!isset($post['type']) || !$post['type']){
            return  $this->renderError('断面类型不能为空');
        }
        if(!isset($post['posname']) || !$post['posname']){
            return  $this->renderError('断面名称不能为空');
        }
        if(!isset($post['com_id']) || !$post['com_id']){
            return  $this->renderError('集团id不能为空');
        }
        if(!isset($post['tasktype_id']) || !$post['tasktype_id']){
            return  $this->renderError('样品类型不能为空');
        }
        if(!isset($post['lng']) || !$post['lng'] ||!isset($post['lat']) || !$post['lat']){
            return  $this->renderError('经纬度不能为空');
        }
        if(!isset($post['province']) || !$post['province']){
            return  $this->renderError('省不能为空');
        }
        if(!isset($post['city']) || !$post['city']){
            return  $this->renderError('市不能为空');
        }
        if(!isset($post['county']) || !$post['county']){
            return  $this->renderError('区县不能为空');
        }

        if(!isset($post['status'])){
            return  $this->renderError('状态不存在');
        }



        $sectionModel = new SectionModel();

        if($sectionModel->insert($post)!==false){
            return $this->renderSuccess();
        }else{
            return  $this->renderError('添加失败');
        }

    }

    //pc断面添加
    public function edit(){
        $post = $this->postData();

        if(!isset($post['id'])  || !$post['id']){
            return  $this->renderError('断面id不能为空');
        }
        if(!isset($post['type']) || !$post['type']){
            return  $this->renderError('断面类型不能为空');
        }
        if(!isset($post['posname']) || !$post['posname']){
            return  $this->renderError('断面名称不能为空');
        }
        if(!isset($post['com_id']) || !$post['com_id']){
            return  $this->renderError('集团id不能为空');
        }
        if(!isset($post['tasktype_id']) || !$post['tasktype_id']){
            return  $this->renderError('样品类型不能为空');
        }
        if(!isset($post['lng']) || !$post['lng'] ||!isset($post['lat']) || !$post['lat']){
            return  $this->renderError('经纬度不能为空');
        }
        if(!isset($post['province']) || !$post['province']){
            return  $this->renderError('省不能为空');
        }
        if(!isset($post['city']) || !$post['city']){
            return  $this->renderError('市不能为空');
        }
        if(!isset($post['county']) || !$post['county']){
            return  $this->renderError('区县不能为空');
        }

        if(!isset($post['status'])){
            return  $this->renderError('状态不存在');
        }



        $sectionModel = new SectionModel();

        if($sectionModel->where(['id'=>$post['id']])->update($post)!==false){
            return $this->renderSuccess();
        }else{
            return  $this->renderError('编辑失败');
        }

    }

    //删除断面
    public function del(){
        $post = $this->postData();
        if(!isset($post['id'])  || !$post['id']){
            return  $this->renderError('断面id不能为空');
        }
        $sectionModel = new SectionModel();
        if($sectionModel->where(['id'=>$post['id']])->delete()!==false){
            return $this->renderSuccess();
        }else{
            return  $this->renderError('删除失败');
        }
    }

}
